const { json } = require("body-parser");
const fs = require("fs")
const path = require("path")
var Crawler = require("crawler");
 
var c = new Crawler({
    maxConnections : 10,
    // This will be called for each crawled page
    callback : function (error, res, done) {
        if(error){
            console.log(error);
        }else{
            var $ = res.$;
            // // $ is Cheerio by default
            // //a lean implementation of core jQuery designed specifically for the server
            // console.log($("title").text());
            // console.log(JSON.parse(res.body))
            JSON.parse(res.body).forEach(item => {
                // console.log(item.ename)
                // console.log(`https://pvp.qq.com/web201605/herodetail/${item.ename}.shtml`)
                xq.queue(`https://pvp.qq.com/web201605/herodetail/${item.ename}.shtml`);
            })
        }
        done();
    }
});
 
// Queue just one URL, with default callback
// c.queue('https://pvp.qq.com/web201605/js/herolist.json');



// // 创建一个详情实例 
// var xq = new Crawler({
//     maxConnections : 10,
//     // This will be called for each crawled page
//     callback : function (error, res, done) {
//         if(error){
//             console.log(error);
//         }else{
//             var $ = res.$;
//             console.log($(".cover-name").text())

            // var data = {
            //     heroName : $(".cover-name").text(),
            //     heroSkill : $(".skill-name>b").first().text(),
            //     heroIcon : "https:" + $(".ico-play").prev("img").attr("src")
            // }
                 
            // fs.appendFile("./uploads/hero.txt", JSON.stringify(data), (err) => {
            //     if (err) {
            //         console.log(err)
            //     }
            //     console.log("文件被保存啦")
            // })
//         }
//         done();
//     }
// });

